<template>
  <view class="goods">
    <block v-for="(item, index) in goodsList" :key="index">
      <MyGoods :item="item" @click.native="toGoodsDetail(item)"></MyGoods>
    </block>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        goodsList: [],
        paginationObj: {
          pageNo: 1,
          pageSize: 10,
          total: 0
        },
        queryObj: {
          query: '',
          cid: ''
        },
        isLoading: false
      };
    },
    methods: {
      async getGoodsList() {
        this.isLoading = true
        const result = await uni.$http.get('/api/public/v1/goods/search', {
          query: this.queryObj.query,
          cid: this.queryObj.cid,
          pagenum: this.paginationObj.pageNo,
          pagesize: this.paginationObj.pageSize
        })
        this.isLoading = false
        if(result.statusCode !== 200) {
          uni.$showMessage()
        }
        else {
          this.paginationObj.total = result.data.message.total
          this.goodsList = [...this.goodsList, ...result.data.message.goods]
        }
      },
      toGoodsDetail(item) {
        uni.navigateTo({
          url: `/subpkg/GoodsDetail/GoodsDetail?goods_id=${item.goods_id}`
        })
      }
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      
      this.getGoodsList()
    },
    onReachBottom() {
      if(this.paginationObj.pageNo * this.paginationObj.pageSize >= this.paginationObj.total) {
        uni.showToast({
          title: '数据加载完毕',
          duration: 1500
        })
        return
      }
      if(this.isLoading) {
        return
      }
      this.paginationObj.pageNo = this.paginationObj.pageNo + 1
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.goodsList = []
      this.paginationObj.pageNo = 1
      this.paginationObj.total = 0
      this.isLoading = false
      this.getGoodsList()
      uni.stopPullDownRefresh()
    }
  }
</script>

<style lang="scss" scoped>

</style>
